<?php

class NGenericObjectCollection{
	var $table_name;
	var $class_name;
	
	var $items_per_page;
	var $item_count=0;
	
	var $id_array;
	
	var $obj_array;
	function &getInstance($table_name,$class_name,$class_type='class'){
		static $instances;
		if (!isset( $instances )) {
				$instances = array();
		}
		$key = $table_name.$class_name.$class_type;
		if (!isset($instances[$key])){
			$instances[$key]=new NGenericObjectCollection($table_name,$class_name,$class_type);
		}
	}
	function __construct($table_name,$class_name,$class_type='class'){
		$this->table_name =$table_name;
		$this->class_name =$class_name;
		$this->class_type=$class_type;
	}
	function AddTuple($id){
		if (!$this->id_array){
			$this->id_array = array();
		}
		array_push($this->id_array,$id);
		$this->item_count = sizeof($this->id_array);
	}
	/**
	* @param $aIds array of objects like obj->id
	*/
	function AddTuples($aIds){
		foreach($aIds as $oId){
			$this->AddTuple($oId->id);
		}
	}
	function SetPageSize($items_per_page){
		$this->items_per_page = $items_per_page;
	}
	function GetItemCount(){
		return $this->item_count;
	}
	function GetNumPages(){
		return(ceil($this->item_count / $this->items_per_page));
	}
	function _GetCommaSeparatedIDList($start_lim = 0,$end_lim = -1){
		$s = "";

		if ($end_lim == -1) {
			$end_lim = sizeof($this->id_array)-1;
		}
		
		
		for ($i=$start_lim;$i<=$end_lim;$i++){
		
		
			if (is_numeric($this->id_array[$i])){
				$s = $s .$this->id_array[$i] . ",";
			}
		}
		$s = substr($s,0, strlen($s)-1);
		
		return $s;
	}
	function _GetIndexFromTupleId($tuple_id){
		$index = -1;
		for ($i=0; $i<=sizeof($this->id_array)-1;$i++){
			if ($this->id_array[$i] == $tuple_id){
				$index =$i;
			}
		}
		return $index;
	}
	function PopulateObjectArray($page_num = 0){
		$items_per_page = $this->items_per_page;
		if ($this->item_count>0){
			if ($page_num >0){
				$start_lim = ($items_per_page*($page_num -1));
				$end_lim = ($start_lim+$items_per_page) -1;
				if ($end_lim>($this->item_count-1)){
					$end_lim = $this->item_count-1;
				}
				
				$_sql = "SELECT * FROM `" .$this->table_name."` WHERE ID IN (" .$this->_GetCommaSeparatedIDList($start_lim,$end_lim). ")";
			} else {
				$_sql = "SELECT * FROM `" .$this->table_name . "` WHERE id IN (" . $this->_GetCommaSeparatedIDList(). ")";
			}
			
			#Выполнение Запроса
			$db = NFactory::getDBO();
			$db->setQuery($_sql);
			$result_rows = $db->loadAssocList();
			for ($i=0;$i<=sizeof($result_rows)-1;$i++){
			
				$this_row = $result_rows[$i];
				$this_db_row_id=$this_row["id"];
				$this_index = $this->_GetIndexFromTupleID($this_db_row_id);
				if ($this_index >= 0){
					$refObjArrayIndexObj = &$this->obj_array[$this_index];

					switch ($this->class_type){
					case'class':
					$s = "\$refObjArrayIndexObj= new " .$this->class_name ."(" .$this_db_row_id. ");";
					eval($s);
					break;
					case'table':
					$refObjArrayIndexObj = NGenericObject::NI($this->class_name,$this_db_row_id);
					break;

					}
					
					
					$refObjArrayIndexObj->ForceLoaded();
					foreach ($this_row as $key=>$value){
						if (!(is_numeric($key))){
							$refObjArrayIndexObj->SetField($key,$value);
						}
					}
				}
			}  
		}
	}
	function RetrievePopulatedObjects($page_num = 0){
		if ($page_num > 0 ){
			$items_per_page = $this->items_per_page;
			#Вычисление начального и конечного ограничения
			$start_lim = ($items_per_page * ($page_num-1));
			$end_lim = ($start_lim +$items_per_page) -1;
				if ($end_lim >($this->item_count-1)){
					$end_lim = $this->item_count -1 ;
				}
		} else {
			$start_lim = 0;
			$end_lim = $this->item_count -1 ;
		}
		$return_array  = array();
		$counter = 0;
			
		for ($i=$start_lim;$i<=$end_lim;$i++){
			$return_array[$counter] = $this->obj_array[$i];
			$counter++;
		}
		return($return_array);
	}
}

?>